﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WD.Classes;
using WDutils;

namespace WD
{
    public partial class QuickAdd : Form
    {
        public QuickAdd()
        {
            InitializeComponent();
        }

        private void quickAdd()
        {
            //do insert into DB.
            //clear data will be made on show.
            ActivityLog objNewActivityLog = new ActivityLog();
            objNewActivityLog.setCreatedOn(Utils.Unix.GetUnixTimestampSecondsFromDateTime(dtQuickAddDate.Value - TimeZone.CurrentTimeZone.GetUtcOffset(dtQuickAddDate.Value)));
            objNewActivityLog.setModifiedOn(Utils.Unix.GetUnixTimestampSecondsFromDateTime(dtQuickAddDate.Value - TimeZone.CurrentTimeZone.GetUtcOffset(dtQuickAddDate.Value)));
            objNewActivityLog.setText(tbQuickAddText.Text);

            ActivityLogEvents.InsertActivityLog eventInsertActivityLog = new ActivityLogEvents.InsertActivityLog(objNewActivityLog);
            eventInsertActivityLog.Publish();
        }

        private void tbQuickAddText_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                quickAdd();

                this.Hide();
            }
            if (e.KeyCode == Keys.Escape)
            {
                this.Hide();
            }
            if (e.Control & e.KeyCode == Keys.Back)
            {
                SendKeys.SendWait("^+{LEFT}{BACKSPACE}");
            }
        }

        private void QuickAdd_Load(object sender, EventArgs e)
        {
            tbQuickAddText.Focus();
        }

        private void QuickAdd_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
        }

        private void b_Quick_In_Click(object sender, EventArgs e)
        {
            tbQuickAddText.Text = ">>in";
            quickAdd();
            this.Hide();
        }

        private void b_Quick_Out_Click(object sender, EventArgs e)
        {
            tbQuickAddText.Text = ">>out";
            quickAdd();
            this.Hide();
        }

        private void QuickAdd_VisibleChanged(object sender, EventArgs e)
        {
            if (this.Visible)
            {
                //Add calculation for getting the passed hours displayed on QuickAdd Caption
                ActivityLogEvents.GetActivityLogCollection evGetActivities = new ActivityLogEvents.GetActivityLogCollection();

                long longTimeStamp_BeginningOfDay = Utils.Unix.GetUnixTimestampSecondsFromDateTime(
                    Utils.Unix.GetDateTime_BeginningOfDay(DateTime.Now));
                long longTimeStamp_EndOfDay = Utils.Unix.GetUnixTimestampSecondsFromDateTime(
                    Utils.Unix.GetDateTime_EndOfDay(DateTime.Now));

                evGetActivities.setTimeFilter_Start(longTimeStamp_BeginningOfDay);
                evGetActivities.setTimeFilter_End(longTimeStamp_EndOfDay);

                List<ActivityLog> listActivityLogs4Export = new List<ActivityLog>();

                listActivityLogs4Export = (List<ActivityLog>)(evGetActivities.Request());
                if (listActivityLogs4Export.Count > 0)
                {
                    //MessageBox.Show(listActivityLogs4Export.Count.ToString());
                    List<ActivityLog> listActivityLogs4Export_Sorted = listActivityLogs4Export.OrderBy(activity => activity.getCreatedOn()).ToList();
                    long longArrivedTime = Utils.Unix.GetUnixTimestampSecondsFromDateTime(listActivityLogs4Export_Sorted[0].getCreatedOnDateTime());
                    long longTimeNow = Utils.Unix.GetUnixTimestampSecondsFromDateTime(DateTime.Now - TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now));

                    //MessageBox.Show(longArrivedTime.ToString() + " " + longTimeNow);

                    double doubleTimePassed = ((longTimeNow - longArrivedTime) / 3600.0);

                    this.Text = "QuickAdd - " + doubleTimePassed.ToString("#.##");
                    L_Quick_TimePassed.Text = "Spent: " + doubleTimePassed.ToString("#.##") + " h";
                    if (doubleTimePassed > 8.0) L_Quick_TimePassed.ForeColor = Color.Red;
                    else L_Quick_TimePassed.ForeColor = Color.Black;

                }
                //END - Add calculation for getting the passed hours displayed on QuickAdd Caption
            }
        }
    }
}
